package cn.pku.sirx.snad.uias.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.Assert;

import cn.pku.sirx.snad.ad.dao.impl.DaoImpl;
import cn.pku.sirx.snad.uias.dao.ISchoolDao;
import cn.pku.sirx.snad.uias.model.entity.School;

/**
 * @author WangXiao
 */
public class SchoolDaoImpl extends DaoImpl<School> implements ISchoolDao {

	public SchoolDaoImpl() {
		super(School.class);
	}

	@Override
	public Map<Integer, List<School>> getByUserId(String userId) {
		Assert.notNull(userId);

		DetachedCriteria criteria = DetachedCriteria.forClass(School.class);
		criteria.add(Restrictions.eq("userId", userId));
		List<School> schools = get(criteria);
		Map<Integer, List<School>> result = new HashMap<Integer, List<School>>();
		for (School school : schools) {
			Integer type = school.getType() == null ? UNKNOWN : school
					.getType();
			List<School> schoolList = result.get(type);
			if (schoolList == null) {
				schoolList = new ArrayList<School>();
				result.put(type, schoolList);
			}
			schoolList.add(school);
		}
		return result;
	}

}
